<template>
  <div class="userManagement">
    <div class="topBar">
      <div class="searchBar">
        <el-form
          ref="queryForm"
          size="small"
          label-width="68px"
          :inline="true"
          :model="queryParams">
          <el-form-item label="用户昵称" prop="nickName">
            <el-input
              v-model="queryParams.nickName"
              placeholder="请输入用户昵称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号码" prop="phonenumber">
            <el-input
              v-model="queryParams.phonenumber"
              placeholder="请输入手机号码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="operateBar">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['userManagement:add']"
        >新增</el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['userManagement:remove']"
        >删除</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['userManagement:export']"
        >导出</el-button>
      </div>
    </div>
    <el-table
      stripe
      height="100%"
      style="width: 100%"
      :data="userList"
      v-loading="loading"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户账号" prop="userName" />
      <el-table-column label="用户昵称" prop="nickName" />
      <el-table-column label="邮箱" prop="email" />
      <el-table-column label="手机号码" prop="phonenumber" />
      <el-table-column label="联系人" prop="company" />
      <el-table-column label="创建人" prop="createBy" />
      <el-table-column label="操作" width="260">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['userManagement:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-resetPwd"
            @click="handleResetPwd(scope.row)"
            v-hasPermi="['userManagement:resetPwd']"
          >重置密码</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-authRole"
            @click="handleAuthRole(scope.row)"
            v-hasPermi="['userManagement:authRole']"
          >用户授权角色</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['userManagement:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改用户对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      append-to-body
      :before-close="closeEvent">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账号" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="联系人" prop="company">
          <el-input v-model="form.company" placeholder="请输入联系人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitEvent">确 定</el-button>
        <el-button @click="closeEvent">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser, resetPwd, authRole } from "@/api/userManagement";
export default {
  name: "User",
  data() {
    return {
      queryParams: {
        nickName: null,
        phonenumber: null,
        pageNum: 1,
        pageSize: 10,
      },

      loading: true,
      ids: [],
      multiple: true,
      total: 0,
      userList: [],

      title: "",
      open: false,
      form: {
        userId: null,
        userName: null,
        nickName: null,
        email: null,
        phonenumber: null,
        company: null,
      },
    };
  },
  computed: {
    rules: function() {
      return {
        userName: [
          { required: true, message: "账号不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "昵称不能为空", trigger: "blur" }
        ],
        phonenumber: [
          { required: true, message: "手机号码不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除用户编号为"' + userIds + '"的数据项？').then(function() {
        return delUser(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 用户授权角色 */
    handleAuthRole(row) {
      const userName = row.userName;
      this.$modal.confirm('是否确认授权用户编号为"' + userName + '"的数据项？').then(function() {
        return authRole(row);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("授权成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },
    /** 重置密码按钮操作 */
    handleResetPwd(row) {
      const {userId} = row;
      this.$prompt('', '重置密码', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValidator: function (value) {
          if(!value) return false;
        },
        inputErrorMessage: '请输入密码'
      }).then(({ value }) => {
        const params = {
          userId,
          password: value
        }
        resetPwd(params).then(response => {
          this.$modal.msgSuccess("重置成功");
          this.getList();
        });
      }).catch(() => {});
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "添加用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const {userId, company} = row;
      getUser(userId).then(response => {
        const {userId, userName, nickName, email, phonenumber} = response.data;
        this.form = {
          userId, userName, nickName, email, phonenumber, company
        };
        this.open = true;
        this.title = "修改用户";
      });
    },
    //新增编辑取消
    closeEvent() {
      this.$refs['form'].resetFields();
      this.open = false;
    },
    //新增编辑提交
    submitEvent() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.closeEvent()
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.closeEvent()
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>
<style lang="scss" scoped>
div.userManagement {
  div.topBar {
    height: 36px;
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    div.searchBar {
      .el-form-item {
        margin-bottom: 0;
      }
    }
  }
}
</style>
